{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "K8S_PROXY_ADDR='127.0.0.1:8001'\n",
    "K8S_NAMESPACE='mdt'\n",
    "APP_NAME='hello-world'\n",
    "MODEL_NAME='sum-model'\n",
    "REPO_URL='658391232643.dkr.ecr.us-west-2.amazonaws.com'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a Clipper Cluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clipper_admin import ClipperConnection, KubernetesContainerManager\n",
    "manager = KubernetesContainerManager(kubernetes_proxy_addr=K8S_PROXY_ADDR,namespace=K8S_NAMESPACE)\n",
    "clipper_conn = ClipperConnection(manager)\n",
    "clipper_conn.start_clipper()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect to an existing cluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clipper_admin import ClipperConnection, KubernetesContainerManager\n",
    "manager = KubernetesContainerManager(kubernetes_proxy_addr=K8S_PROXY_ADDR,namespace=K8S_NAMESPACE)\n",
    "clipper_conn = ClipperConnection(manager)\n",
    "clipper_conn.connect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an Application"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clipper_conn.register_application(name=APP_NAME, input_type=\"doubles\", default_output=\"-1.0\", slo_micros=100000)\n",
    "print clipper_conn.get_all_apps()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As the K8s cluster is on AWS, using ECR as the container repo here, need login to ECR first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!$(aws ecr get-login --no-include-email --region us-west-2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a simple python predict function and deploy it, note, the repo named `default-cluster-<model-name>` need be created in advance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clipper_admin.deployers import python as python_deployer\n",
    "from clipper_admin import ClipperConnection, KubernetesContainerManager\n",
    "\n",
    "manager = KubernetesContainerManager(kubernetes_proxy_addr=K8S_PROXY_ADDR,namespace=K8S_NAMESPACE)\n",
    "clipper_conn = ClipperConnection(manager)\n",
    "clipper_conn.connect()\n",
    "def feature_sum(xs):\n",
    "    return [str(sum(x)) for x in xs]\n",
    "\n",
    "python_deployer.deploy_python_closure(clipper_conn, \n",
    "                                      name=MODEL_NAME, \n",
    "                                      version=2, \n",
    "                                      input_type=\"doubles\", \n",
    "                                      func=feature_sum,\n",
    "                                     registry=REPO_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Link the mode to the application will create an endpoint and then you can call the REST.  `http://<query_address>/<application-name>/predict`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clipper_conn.link_model_to_app(app_name=APP_NAME, model_name=MODEL_NAME)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Call REST post to do the inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json, numpy as np\n",
    "url = \"http://{}/api/v1/namespaces/{}/services/query-frontend-at-default-cluster:1337/proxy/{}/predict\".format(K8S_PROXY_ADDR,K8S_NAMESPACE,APP_NAME)\n",
    "# or clipper_conn.get_query_addr()\n",
    "headers = {\"Content-type\": \"application/json\"}\n",
    "data = json.dumps({\"input\": list(np.random.random(10))})\n",
    "print(data)\n",
    "requests.post(url, headers=headers, data=data).json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List all models and apps with detailed information here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for app in clipper_conn.get_all_apps(verbose=True):\n",
    "    print json.dumps(app, indent=2)\n",
    "for model in clipper_conn.get_all_models(verbose=True):\n",
    "    print json.dumps(model, indent=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Stop Models and Applications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clipper_conn.stop_models(MODEL_NAME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clipper_conn.delete_application(APP_NAME)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "stop the clipper cluster will delete all the deployment of clipper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from clipper_admin import ClipperConnection, KubernetesContainerManager\n",
    "manager = KubernetesContainerManager(kubernetes_proxy_addr='127.0.0.1:8001',namespace='mdt')\n",
    "clipper_conn = ClipperConnection(manager)\n",
    "clipper_conn.stop_all()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
